<template>
  <div id="app" style="height: 100%;">
    <router-view/>
  </div>
</template>

<script>
  import storage from "./store/storage.js";
  import wx from "weixin-js-sdk";
  import http from "./http";

  export default {
    name: 'App',
    watch: {
      // 如果路由有变化，会再次执行该方法
      '$route': 'fetchData'
    },
    created: function () {

      const housingId = storage.get("housingId", "local");
      if (housingId) {
        storage.put("housingId", housingId)
      }

    },
    methods: {

      // ?appId=wxe2f71ea9af8ea232

      fetchData: function () {
        if (this.$route.query['x-access-token']) {
          const token = this.$route.query['x-access-token'];
          if (token instanceof Array) {
            storage.put("token", token[0]);
          } else {
            storage.put("token", token);
          }
        }
        if (this.$route.query.appId) {
          storage.put("appId", this.$route.query.appId, 'local');
        }
        if  (this.$route.query.housingId) {
          storage.put("house-id", this.$route.query.housingId);
        }
        this.getWxConfig();
      },
      getWxConfig: function () {

        const appId = storage.get("appId", 'local');
        http.get("/weixin/auth/wxJssdkConfig?appId=" + appId).then(res => {
          // console.log(res.data)
          if (res.data.code == 20000 && res.data.data) {
            // this.wxConfig = res.data.data;
            wx.config({
              debug: false,
              appId: res.data.data.appId,
              timestamp: res.data.data.timestamp,
              nonceStr: res.data.data.nonceStr,
              signature: res.data.data.signature,
              jsApiList: ['chooseWXPay']
            });
          }
        });
      },


    }
  }
</script>

<style>
  #app {
    font-family: 'Avenir', Helvetica, Arial, sans-serif;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
  }
</style>
